<?php
/**
 * Created by PhpStorm.
 * User: yaosj
 * Date: 15-7-10
 * Time: 下午12:19
 */

class GetUserInfo implements iController {

    /**
     * @param array $queryArgs
     * @return array
     */
    public function output($queryArgs) {
        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        //获取用户信息
        $userInfo = getUserInfo($queryArgs['user_id']);

        //面试开启状态
        $interviewEnabled = false;
        if ($this->checkInterviewStatus($queryArgs['user_id'])) {
            $interviewEnabled = true;
        }

        return genOutput(1, array(
            'is_new' => false,
            'user' => array(
                'user_id' => intval($queryArgs['user_id']),
                'user_token' => $queryArgs['user_token'],
                'nickname' => strval($userInfo->nickname),
                'mobile_num' => empty($userInfo->phone) ? '' : $userInfo->phone,
                'avatar' => strval($userInfo->avatar),
                'area' => '',
                'birthday' => '',
                'career' => '',
                'mail' => strval($userInfo->user_email),
                'sno' => intval($userInfo->user_sno),
                'address' => strval($userInfo->address),
                'weibo' => strval($userInfo->weibo),
                'weixin' => strval($userInfo->weixin),
                'related_phone' => empty($userInfo->related_phone) ? '' : $userInfo->related_phone,
                'enable_interview' => $interviewEnabled,
                'qq' => (string)$userInfo->qq,
                'alipay_id' => (string)$userInfo->alipay_id,
                'alipay_name' => (string)$userInfo->alipay_name
            ),
            'exam_info' => $this->getExamInfo($queryArgs['user_id'], $queryArgs['app_type'])
        ));
    }

    /**
     * 检测面试开启情况
     *
     * @param int $userId 用户id
     * @return null|string
     */
    private function checkInterviewStatus($userId) {
        global $cmsdb;

        return $cmsdb->getVar('SELECT `id` FROM `relation_user_product` WHERE `user_id`=:uid AND `product_id`=1128 AND `product_type`="interview"',
            array(':uid' => $userId));
    }

    /**
     * 获取用户的考试项目信息
     *
     * @param int $userId 用户id
     * @return array|stdClass 考试信息
     */
    private function getExamInfo($userId, $appType) {
        global $uddb, $cmsdb;

        switch ($appType) {
            case 'dailyplan':
                //检验是否开启上岸计划
                $sql = "SELECT `testarea_id` FROM `{$uddb->userDPData}` WHERE `user_id`={$userId}";
                $testId = $uddb->getVar($sql);

                //examId到examDetailId的转换
                $today = date('Y-m-d');
                $sql2 = "SELECT `id` FROM `data_exam_detail` WHERE `exam_id`=:eid AND `type`='笔试' AND `exam_day`>'{$today}' ORDER BY `exam_day` ASC LIMIT 1";
                $testId = $cmsdb->getVar($sql2, [':eid' => $testId]);

                break;

            case 'quizbank':
                //检验是否开启了天天模考
                $sql = "SELECT `exam_detail_id` FROM `qb_user_data` WHERE `user_id`={$userId}";
                $testId = $cmsdb->getVar($sql);
                break;

            case 'dailylearn':
                $sql = "SELECT `test_id` FROM `user_social_info` WHERE `user_id`={$userId}";
                $testId = $uddb->getVar($sql);
                break;

            default:
                break;
        }

        //考试项目信息
        if (!empty($testId)) {
            return getExamDetail($testId);
        }

        return new stdClass();
    }
}